package com.changgou.seckill.thread;

import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import entity.IdWorker;
import entity.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 描述
 *
 * @author ljh
 * @version 1.0
 * @package com.changgou.seckill.thread *
 * @Date 2019-12-10
 * @since 1.0
 */
@Component
public class CreateOrderThread {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private IdWorker idWorker;

    //开启异步调用底层就是线程池多线程
    @Async
    public void createOrder(){


        System.out.println("=============下单开始:线程"+Thread.currentThread().getName()+"============================"+new Date());

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("=============下单结束:线程"+Thread.currentThread().getName()+"============================"+new Date());

        //从队列中获取抢单的信息
        SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps(SystemConstants.SEC_KILL_USER_QUEUE_KEY).rightPop();

        if(seckillStatus!=null) {

            String time =seckillStatus.getTime();
            Long id = seckillStatus.getGoodsId();
            String username = seckillStatus.getUsername();


            //1.根据商品的ID 获取商品的数据
            //redisTemplate.boundHashOps("SeckillGoods_")
            SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).get(id);
            //2.判断商品是否存在或者商品的库存是否为0  如果是 ,返回,售罄了

            Object o = redisTemplate.boundListOps(SystemConstants.SEC_KILL_CHAOMAI_LIST_KEY_PREFIX + id).rightPop();
            if(o==null){
                throw new RuntimeException("商品卖完了");
            }
          /*  if (seckillGoods == null || seckillGoods.getStockCount() <= 0) {
                //XXXexcpe
                //YYYexpcetion
                throw new RuntimeException("商品卖完了");
            }*/

            //3.先减库存
            seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
            redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).put(id, seckillGoods);
            //4.判断是否库存为0 ,如果为0 更新到数据库中,删除掉原来的商品.
            if (seckillGoods.getStockCount() <= 0) {
                seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
                redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).delete(id);//
            }
            //5.创建预订单
            SeckillOrder order = new SeckillOrder();
            order.setId(idWorker.nextId());//设置订单的iD
            order.setSeckillId(id);//购买的商品的ID
            order.setMoney(seckillGoods.getCostPrice());//设置购买的金额
            order.setUserId(username);//设置订单所属的用户
            order.setCreateTime(new Date());//设置创建时间
            order.setStatus("0");//未支付
            redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).put(username, order);//key field value  key:大key,field:某一个用户的 value:订单
            //下单成功修改用户抢单的状态
            seckillStatus.setStatus(2);//抢单成功,待支付的状态
            seckillStatus.setOrderId(order.getId());
            seckillStatus.setMoney(Float.valueOf(order.getMoney()));
            redisTemplate.boundHashOps(SystemConstants.SEC_KILL_USER_STATUS_KEY).put(username,seckillStatus);
        }
    }
}
